* Merge three rounds of survey data and create final variables for analysis

use "${output}r0_baseline_cleaned.dta", clear
append using "${output}r1_followup_cleaned.dta"
append using "${output}r2_followup_cleaned.dta"

********************************************************************************

* BASIC VARIABLE SET-UP

sort hh_id surveyround

foreach var of varlist state_code districtcode districtname village gpsubcl_code uniquegrp blockcode blockname {
	bys hh_id (surveyround) : replace `var' = `var'[_n-1] if _n == 2 & !mi(`var'[_n-1])
	bys hh_id (surveyround) : replace `var' = `var'[_n-1] if _n == 3 & !mi(`var'[_n-1])
	}

egen treat_mean = mean(treatment), by(hh_id)
egen chirag_mean = mean(chirag_strata), by(hh_id)
drop treatment chirag_strata
ren treat_mean treatment
lab var treatment "Household randomly assigned to promotion intervention"
ren chirag_mean chirag_strata
lab var chirag_strata "GP has NGO presence"

********************************************************************************

* DESCRIPTIVE STATISTICS

global personid 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

* Number of household members indicated as 'head'
egen count_hh = anycount(household_head_*), values(1)
lab var count_hh "Number of household members indicated as 'head'"

* Number of household members indicated as 'primary cook'
egen count_pc = anycount(cooks_*), values(1)
lab var count_pc "Number of household members indicated as 'primary cook'"

* Household size
egen household_size = rownonmiss(*_member_*), strok
label var household_size "Household size"
tab household_size

* Number of children under five
foreach i in $personid {
	replace age_year_`i' = . if age_year_`i' < 0
	replace age_month_`i' = . if !mi(age_year_`i') & age_year_`i' > 5
	replace age_day_`i' = . if !mi(age_year_`i') & age_year_`i' > 5
	gen child_under_five_`i' = 0
	replace child_under_five_`i' = 1 if (age_year_`i' <= 5 & !mi(age_year_`i')) | (age_month_`i' > 0 & !mi(age_month_`i')) | (age_day_`i' > 0 & !mi(age_day_`i'))
	replace child_under_five_`i' = . if mi(age_year_`i') & mi(age_month_`i') & mi(age_day_`i')
	}
egen children_under_five = rowtotal(child_under_five_*)

* Female head of household
foreach i in $personid {
	gen female_hh_`i' = 0 if household_head_`i' == 1 & sex_`i' != 2
	replace female_hh_`i' = 1 if household_head_`i' == 1 & sex_`i' == 2
	}
egen female_household_head = rowtotal(female_hh_*), missing
replace female_household_head = 1 if female_household_head >= 1 & !mi(female_household_head)
label var female_household_head "Head of household is female"
drop female_hh_*

* Age of head of household
foreach i in $personid {
	gen age_hh_`i' = age_year_`i' if household_head_`i' == 1
	}
rowsort age_hh_*, gen(s1 - s15)
gen age_household_head = .
foreach i of numlist 1/15 {
	replace age_household_head = s`i' if count_hh == `i'
	}
replace age_household_head = . if age_household_head < 0 
label var age_household_head "Age of head of household (years)"
drop age_hh_* s1 - s15

* Years of education of head of household
foreach i in $personid {
	gen educ_hh_`i' = education_years_`i' if household_head_`i' == 1 
	replace educ_hh_`i' = 0 if school_indicator_`i'== 0 & household_head_`i' == 1 
	replace educ_hh_`i' = . if (education_years_`i' == -9 & household_head_`i' == 1) | (school_indicator_`i' == -9 & household_head_`i' == 1)
	}
rowsort educ_hh_*, gen(s1 - s15)
gen education_household_head = .
foreach i of numlist 1/15 {
	replace education_household_head = s`i' if count_hh == `i'
	} 
label var education_household_head "Years of education - head of household"
drop educ_hh_* s1 - s15

* Years of education of primary cook
foreach i in $personid {
	gen educ_pc_`i' = education_years_`i' if cooks_`i' == 1
	replace educ_pc_`i' = 0 if school_indicator_`i' == 0 & cooks_`i' == 1
	replace educ_pc_`i' = . if (education_years_`i' == -9 & cooks_`i' == 1) | (school_indicator_`i' == -9 & cooks_`i' == 1)
	}
rowsort educ_pc_*, gen(s1 - s15)
gen education_primary_cook = .
foreach i of numlist 1/15 {
	replace education_primary_cook = s`i' if count_pc == `i'
	} 
drop educ_pc_* s1 - s15
label var education_primary_cook "Years of education - primary cook"

* Below poverty line (BPL)
label var below_pov_line "Household reports they are below poverty line (BPL)"

* Owns traditional stove (BASELINE)
gen traditional_stove_own = 0 if BL_stove_own_01 == 0 | BL_stove_own_02 == 0 | BL_stove_own_03 == 0 | BL_stove_own_09 == 0 | BL_stove_own_10 == 0
replace traditional_stove_own = 1 if BL_stove_own_01 == 1 | BL_stove_own_02 == 1 | BL_stove_own_03 == 1 | BL_stove_own_09 == 1 | BL_stove_own_10 == 1
replace traditional_stove_own = 1 if BL_number_stove_own_01 == 1 & traditional_stove_own == .		// N = 2 households
replace traditional_stove_own = 0 if BL_stove_own_06 == 1 & traditional_stove_own == .				// N = 1 households
* Owns traditional stove (FOLLOW-UP 1)
replace traditional_stove_own = 0 if R1_stove_own_01 == 0 | R1_stove_own_03 == 0 | R1_stove_own_09 == 0 
replace traditional_stove_own = 1 if R1_stove_own_01 == 1 | R1_stove_own_03 == 1 | R1_stove_own_09 == 1
replace traditional_stove_own = 0 if R1_stove_own_04 == 1 & traditional_stove_own == . 				// N = 1 household
* Owns tradititional stove (FOLLOW-UP 2)
foreach i in 01 01an2 03 07bn {
	replace R2_stove_own_`i' = "1" if R2_stove_own_`i' == "0,1" | R2_stove_own_`i' == "0, 1"
	destring R2_stove_own_`i', replace
	}
foreach i in 01 09 {
	replace R2_stove_own_`i' = 1 if R2_stove_own_`i' == 0 & R2_number_stove_own_`i' == "1"
	}
replace R2_number_stove_own_01 = "1" if R2_number_stove_own_01 == "1,2"
replace R2_number_stove_own_09 = "1" if R2_number_stove_own_09 == "1, 2"
destring R2_number_stove_own_*, replace	
foreach i in 01 01an2 03 04 04bn2 06 07an2 07bn 07cn2 07dn2 07en2 08 09 11an 11bn 12 {
	replace R2_stove_own_`i' = 1 if R2_stove_own_`i' == 0 & (R2_number_stove_own_`i' > 0 & !mi(R2_number_stove_own_`i'))
	}
replace traditional_stove_own = 0 if R2_stove_own_01 == 0 | R2_stove_own_01an2 == 0 | R2_stove_own_03 == 0 | R2_stove_own_09 == 0
replace traditional_stove_own = 1 if R2_stove_own_01 == 1 | R2_stove_own_01an2 == 1 | R2_stove_own_03 == 1 | R2_stove_own_09 == 1
replace traditional_stove_own = 0 if R2_stove_own_04 == 1 & traditional_stove_own == .
label var traditional_stove_own "HH owns trad. stove (mitti ka chulha, anjeti, TSF, sagarh, coal)"

* Minutes of traditional-stove use per day (BASELINE)
foreach i in 01 02 03 04 05 06 07 08 09 10 11 12 {
	destring BL_stove_use_hours_`i' BL_stove_use_minutes_`i', replace
	
	replace BL_stove_use_hours_`i' = 0 if BL_stove_use_hours_`i' < 0 & !mi(BL_stove_use_hours_`i')
	replace BL_stove_use_hours_`i' = 24 if BL_stove_use_hours_`i' > 24 & !mi(BL_stove_use_hours_`i')
	replace BL_stove_use_minutes_`i' = 0 if BL_stove_use_minutes_`i' < 0 & !mi(BL_stove_use_minutes_`i')
	replace BL_stove_use_minutes_`i' = 1440 if BL_stove_use_minutes_`i' > 1440 & !mi(BL_stove_use_minutes_`i')
	
	gen BL_stove_hours_to_mins_`i' = BL_stove_use_hours_`i' * 60
	egen BL_total_minutes_stove_`i' = rowtotal(BL_stove_hours_to_mins_`i' BL_stove_use_minutes_`i'), missing
	replace BL_total_minutes_stove_`i' = 0 if BL_stove_own_`i' == 0
	lab var BL_total_minutes_stove_`i' "BL: Total minutes using stove [TYPE] per day"
	lab var BL_stove_hours_to_mins_`i' "BL: Total hours using stove [TYPE] (in mins.)"
	}
* Minutes of traditional-stove use per day (FOLLOW-UP 1)
foreach i in 01 03 04 06 07an 07bn 08 09 11an 11bn 12 {
	destring R1_stove_use_hours_`i' R1_stove_use_minutes_`i', replace
	
	replace R1_stove_use_hours_`i' = 0 if R1_stove_use_hours_`i' < 0 & !mi(R1_stove_use_hours_`i')
	replace R1_stove_use_hours_`i' = 24 if R1_stove_use_hours_`i' > 24 & !mi(R1_stove_use_hours_`i')
	replace R1_stove_use_minutes_`i' = 0 if R1_stove_use_minutes_`i' < 0 & !mi(R1_stove_use_minutes_`i')
	replace R1_stove_use_minutes_`i' = 1440 if R1_stove_use_minutes_`i' > 1440 & !mi(R1_stove_use_minutes_`i')
	
	gen R1_stove_hours_to_mins_`i' = R1_stove_use_hours_`i' * 60
	egen R1_total_minutes_stove_`i' = rowtotal(R1_stove_hours_to_mins_`i' R1_stove_use_minutes_`i'), missing
	replace R1_total_minutes_stove_`i' = 0 if R1_stove_own_`i' == 0
	lab var R1_total_minutes_stove_`i' "R1: Total minutes using stove [TYPE] per day"
	lab var R1_stove_hours_to_mins_`i' "R1: Total hours using stove [TYPE] (in mins.)"
	}
* Minutes of traditional-stove use per day (FOLLOW-UP 2)
foreach i in 01 01an2 03 04 04bn2 06 07an2 07bn 07cn2 07dn2 07en2 08 09 11an 11bn 12 {
	capture replace R2_stove_use_hours_`i' = "0.5" if R2_stove_use_hours_`i' == "1/2" | R2_stove_use_hours_`i' == "1/2 hours"
	capture replace R2_stove_use_hours_`i' = "0.2" if R2_stove_use_hours_`i' == "1/5"
	capture replace R2_stove_use_minutes_`i' = "0.5" if R2_stove_use_minutes_`i' == "1/2" 
	destring R2_stove_use_hours_`i' R2_stove_use_minutes_`i', replace
	
	replace R2_stove_use_hours_`i' = 0 if R2_stove_use_hours_`i' < 0 & !mi(R2_stove_use_hours_`i')
	replace R2_stove_use_hours_`i' = 24 if R2_stove_use_hours_`i' > 24 & !mi(R2_stove_use_hours_`i')
	replace R2_stove_use_minutes_`i' = 0 if R2_stove_use_minutes_`i' < 0 & !mi(R2_stove_use_minutes_`i')
	replace R2_stove_use_minutes_`i' = 1440 if R2_stove_use_minutes_`i' > 1440 & !mi(R2_stove_use_minutes_`i')
	
	gen R2_stove_hours_to_mins_`i' = R2_stove_use_hours_`i' * 60
	egen R2_total_minutes_stove_`i' = rowtotal(R2_stove_hours_to_mins_`i' R2_stove_use_minutes_`i'), missing
	replace R2_total_minutes_stove_`i' = 0 if R2_stove_own_`i' == 0
	lab var R2_total_minutes_stove_`i' "R2: Total minutes using stove [TYPE] per day"
	lab var R2_stove_hours_to_mins_`i' "R2: Total hours using stove [TYPE] (in mins.)"
	}
egen total_minutes_traditional_stove = rowtotal(BL_total_minutes_stove_01 BL_total_minutes_stove_02 BL_total_minutes_stove_03 BL_total_minutes_stove_09 BL_total_minutes_stove_10 ///
	R1_total_minutes_stove_01 R1_total_minutes_stove_03 R1_total_minutes_stove_09 ///
	R2_total_minutes_stove_01 R2_total_minutes_stove_01an2 R2_total_minutes_stove_03 R2_total_minutes_stove_09), missing
winsor2 total_minutes_traditional_stove, replace cuts(0 97.5) by(surveyround)
label var total_minutes_traditional_stove "Traditional-stove use (chulha, anjeti, TSF, sagarh, coal), minutes per day"

* Heard of stoves that produce less smoke
gen heard_of_clean_stoves = 0 if BL_heard_stove_lesssmoke == 0 | BL_heard_stove_lesssmoke2 == 0 | BL_heard_stove_lesssmoke3 == 0 | ///
	R1_heard_stove_lesssmoke == 0 
replace heard_of_clean_stoves = 1 if BL_heard_stove_lesssmoke == 1 | BL_heard_stove_lesssmoke2 == 1 | BL_heard_stove_lesssmoke3 == 1 | ///
	R1_heard_stove_lesssmoke == 1
replace heard_of_clean_stoves = 0 if heard_of_clean_stoves == . & surveyround == 0
	
* Heard of fuels that produce less smoke
gen heard_of_clean_fuels = 0 if BL_heard_fuel_lesssmoke == 0 | BL_heard_fuel_lesssmoke2 == 0 | BL_heard_fuel_lesssmoke3 == 0 | ///
	R1_heard_fuel_lesssmoke == 0
replace heard_of_clean_fuels = 1 if BL_heard_fuel_lesssmoke == 1 | BL_heard_fuel_lesssmoke2 == 1 | BL_heard_fuel_lesssmoke3 == 1 | ///
	R1_heard_fuel_lesssmoke == 1
replace heard_of_clean_fuels = 0 if heard_of_clean_fuels == . & surveyround == 0

* Smoke safety perception
gen smoke_unsafe = 1 if smoke_safety_perception <= 4 & smoke_safety_perception != -99 & !mi(smoke_safety_perception)
replace smoke_unsafe = 0 if smoke_safety_perception > 4 & !mi(smoke_safety_perception)
label var smoke_unsafe "Household thinks smoke is unsafe (4 or fewer candies in safe pile)"

* Purchase intervention stove
gen purchased_intervention_stove = 1 if (BuyGcoil == 1 | BuyGway == 1) & surveyround == 1
egen mean_purchased = mean(purchased_intervention_stove), by(hh_id)
tab mean_purchased
replace purchased_intervention_stove = mean_purchased if surveyround == 2
replace purchased_intervention_stove = 0 if mi(purchased_intervention_stove)
lab var purchased_intervention_stove "Purchased an intervention stove"
drop mean_purchased

* Owns improved stove (BASELINE) (unadjusted)
gen improved_stove_own = 0 if BL_stove_own_04 == 0 | BL_stove_own_05 == 0 | BL_stove_own_06 == 0 | BL_stove_own_07 == 0 | BL_stove_own_08 == 0 | BL_stove_own_11 == 0
replace improved_stove_own = 1 if BL_stove_own_04 == 1 | BL_stove_own_05 == 1 | BL_stove_own_06 == 1 | BL_stove_own_07 == 1 | BL_stove_own_08 == 1 | BL_stove_own_11 == 1
replace improved_stove_own = 0 if improved_stove_own == . & surveyround == 0
* Owns improved stove (FOLLOW-UP 1)
replace improved_stove_own = 0 if R1_stove_own_04 == 0 | R1_stove_own_06 == 0 | R1_stove_own_07an == 0 | R1_stove_own_07bn == 0 | R1_stove_own_08 == 0 | R1_stove_own_11an == 0 | R1_stove_own_11bn == 0
replace improved_stove_own = 1 if R1_stove_own_04 == 1 | R1_stove_own_06 == 1 | R1_stove_own_07an == 1 | R1_stove_own_07bn == 1 | R1_stove_own_08 == 1 | R1_stove_own_11an == 1 | R1_stove_own_11bn == 1
replace improved_stove_own = 0 if improved_stove_own == . & lostin2013 == 0
* Owns improved stove (FOLLOW-UP 2)
replace improved_stove_own = 0 if R2_stove_own_04 == 0 | R2_stove_own_04bn2 == 0 | R2_stove_own_06 == 0 | R2_stove_own_07an2 == 0 | R2_stove_own_07bn == 0 | ///
	R2_stove_own_07cn2 == 0 | R2_stove_own_07dn2 == 0 | R2_stove_own_07en2 == 0 | R2_stove_own_08 == 0 | R2_stove_own_11an == 0 | R2_stove_own_11bn == 0
replace improved_stove_own = 1 if R2_stove_own_04 == 1 | R2_stove_own_04bn2 == 1 | R2_stove_own_06 == 1 | R2_stove_own_07an2 == 1 | R2_stove_own_07bn == 1 | ///
	R2_stove_own_07cn2 == 1 | R2_stove_own_07dn2 == 1 | R2_stove_own_07en2 == 1 | R2_stove_own_08 == 1 | R2_stove_own_11an == 1 | R2_stove_own_11bn == 1
replace improved_stove_own = 0 if improved_stove_own == . & lostin2014 == 0
label var improved_stove_own "HH owns improved stove (kero., LPG, elec., biogas, comm. ICS, G-Coil/Greenway)"

* Uses improved stove (BASELINE)
gen improved_stove_use = 0 if BL_stove_used_04 == 0 | BL_stove_used_05 == 0 | BL_stove_used_06 == 0 | BL_stove_used_07 == 0 | BL_stove_used_08 == 0 | BL_stove_used_11 == 0
replace improved_stove_use = 1 if BL_stove_used_04 == 1 | BL_stove_used_05 == 1 | BL_stove_used_06 == 1 | BL_stove_used_07 == 1 | BL_stove_used_08 == 1 | BL_stove_used_11 == 1
replace improved_stove_use = 0 if improved_stove_own == 0 & surveyround == 0
* Uses improved stove (FOLLOW-UP 1)
replace improved_stove_use = 0 if R1_stove_used_04 == 0 | R1_stove_used_06 == 0 | R1_stove_used_07an == 0 | R1_stove_used_07bn == 0 | R1_stove_used_08 == 0 | R1_stove_used_11an == 0 | R1_stove_used_11bn == 0
replace improved_stove_use = 1 if R1_stove_used_04 == 1 | R1_stove_used_06 == 1 | R1_stove_used_07an == 1 | R1_stove_used_07bn == 1 | R1_stove_used_08 == 1 | R1_stove_used_11an == 1 | R1_stove_used_11bn == 1
replace improved_stove_use = 0 if improved_stove_own == 0 & surveyround == 1
replace improved_stove_use = 0 if improved_stove_use == . & lostin2013 == 0
* Uses improved stove (FOLLOW-UP 2)
replace improved_stove_use = 0 if R2_stove_used_04 == 0 | R2_stove_used_04bn2  == 0 | R2_stove_used_06  == 0 | R2_stove_used_07an2  == 0 | R2_stove_used_07bn == 0 | R2_stove_used_07cn2 == 0 | R2_stove_used_07dn2 == 0 | ///
	R2_stove_used_07en2 == 0 | R2_stove_used_08 == 0 | R2_stove_used_11an == 0 | R2_stove_used_11bn == 0
replace improved_stove_use = 1 if R2_stove_used_04 == 1 | R2_stove_used_04bn2  == 1 | R2_stove_used_06  == 1 | R2_stove_used_07an2  == 1 | R2_stove_used_07bn == 1 | R2_stove_used_07cn2 == 1 | R2_stove_used_07dn2 == 1 | ///
	R2_stove_used_07en2 == 1 | R2_stove_used_08 == 1 | R2_stove_used_11an == 1 | R2_stove_used_11bn == 1
replace improved_stove_use = 0 if improved_stove_own == 0 & surveyround == 2
replace improved_stove_use = 0 if improved_stove_use == . & lostin2014 == 0
label var improved_stove_use "HH used improved stove in past week"

* Owns LPG stove (BASELINE)
gen lpg_stove_own = 0 if BL_stove_own_06 == 0
replace lpg_stove_own = 1 if BL_stove_own_06 == 1
replace lpg_stove_own = 0 if lpg_stove_own == . & surveyround == 0
* Owns LPG stove (FOLLOW-UP 1)
replace lpg_stove_own = 0 if R1_stove_own_06 == 0
replace lpg_stove_own = 1 if R1_stove_own_06 == 1
replace lpg_stove_own = 0 if lpg_stove_own == . & lostin2013 == 0
* Owns LPG stove (FOLLOW-UP 2)
replace lpg_stove_own = 0 if R2_stove_own_06 == 0
replace lpg_stove_own = 1 if R2_stove_own_06 == 1
replace lpg_stove_own = 0 if lpg_stove_own == . & lostin2014 == 0
lab var lpg_stove_own "HH owns LPG stove"

* Uses LPG stove (BASELINE)
gen lpg_stove_use = 0 if BL_stove_used_06 == 0
replace lpg_stove_use = 1 if BL_stove_used_06 == 1
replace lpg_stove_use = 0 if lpg_stove_own == 0 & surveyround == 0
replace lpg_stove_use = 0 if lpg_stove_use == . & surveyround == 0
* Uses LPG stove (FOLLOW-UP 1)
replace lpg_stove_use = 0 if R1_stove_used_06 == 0
replace lpg_stove_use = 1 if R1_stove_used_06 == 1
replace lpg_stove_use = 0 if lpg_stove_own == 0 & surveyround == 1
replace lpg_stove_use = 0 if lpg_stove_use == . & lostin2013 == 0
* Uses LPG stove (FOLLOW-UP 2)
replace lpg_stove_use = 0 if R2_stove_used_06 == 0
replace lpg_stove_use = 1 if R2_stove_used_06 == 1
replace lpg_stove_use = 0 if lpg_stove_own == 0 & surveyround == 2
replace lpg_stove_use = 0 if lpg_stove_use == . & lostin2014 == 0
lab var lpg_stove_use "HH used LPG stove in past week"

* Owns intervention stove (unadjusted)
gen intervention_stove_own = 0  if surveyround == 0 | treatment == 0
replace intervention_stove_own = 0 if R1_stove_own_07bn == 0 | R1_stove_own_11an == 0
replace intervention_stove_own = 1 if R1_stove_own_07bn == 1 | R1_stove_own_11an == 1
replace intervention_stove_own = 0 if intervention_stove_own == . & lostin2013 == 0 
replace intervention_stove_own = 0 if R2_stove_own_07bn == 0 | R2_stove_own_11an == 0
replace intervention_stove_own = 1 if R2_stove_own_07bn == 1 | R2_stove_own_11an == 1
replace intervention_stove_own = 0 if intervention_stove_own == . & lostin2014 == 0 
lab var intervention_stove_own "HH owns intervention stove"

* Uses intervention stove (BASELINE) (unadjusted)
gen intervention_stove_use = 0 if surveyround == 0 | treatment == 0
* Uses intervention stove (FOLLOW-UP 1) (unadjusted)
replace intervention_stove_use = 0 if R1_stove_used_07bn == 0 | R1_stove_used_11an == 0 
replace intervention_stove_use = 1 if R1_stove_used_07bn == 1 | R1_stove_used_11an == 1 
replace intervention_stove_use = 0 if intervention_stove_own == 0 & surveyround == 1
replace intervention_stove_use = 0 if intervention_stove_use == . & lostin2013 == 0
* Uses intervention stove (FOLLOW-UP 2) (unadjusted)
replace intervention_stove_use = 0 if R2_stove_used_07bn == 0 | R2_stove_used_11an == 0
replace intervention_stove_use = 1 if R2_stove_used_07bn == 1 | R2_stove_used_11an == 1
replace intervention_stove_use = 0 if intervention_stove_own == 0 & surveyround == 2
replace intervention_stove_use = 0 if intervention_stove_use == . & lostin2014 == 0
lab var intervention_stove_use "HH used intervention stove in past week"

* Uses traditional fuel
gen used_traditional_fuel = 0 if fuel_use_01 == 0 | fuel_use_02 == 0 | fuel_use_03 == 0 | fuel_use_04 == 0 | fuel_use_11n2 == 0
replace used_traditional_fuel = 1 if fuel_use_01 == 1 | fuel_use_02 == 1 | fuel_use_03 == 1 | fuel_use_04 == 1 | fuel_use_11n2 == 1
label var used_traditional_fuel "HH uses traditional fuel (fuelwood, crop res., leaves, dung, trash)"

* Use of clean fuels, and regularity of clean-fuel use
gen clean_fuel_use = 0 if fuel_use_06 == 0 | fuel_use_07 == 0 | fuel_use_08 == 0 | fuel_use_09 == 0
replace clean_fuel_use = 1 if fuel_use_06 == 1 | fuel_use_07 == 1 | fuel_use_08 == 1 | fuel_use_09 == 1
lab var clean_fuel_use "HH uses clean fuel (kerosene, LPG, electricity, and/or biogas)"
gen clean_fuel_daily = 0 if fuel_use_regularity_06 == 0 | fuel_use_regularity_07 == 0 | fuel_use_regularity_08 == 0 | fuel_use_regularity_09 == 0
replace clean_fuel_daily = 1 if fuel_use_regularity_06 == 1 | fuel_use_regularity_07 == 1 | fuel_use_regularity_08 == 1 | fuel_use_regularity_09 == 1
replace clean_fuel_daily = 0 if clean_fuel_use == 0
replace clean_fuel_daily = 0 if improved_stove_own == 0
lab var clean_fuel_daily "HH uses clean fuel (kerosene, LPG, electricity, and/or biogas) daily"

* Time spent collecting fuels
replace fuel_collection_minutes_07 = 60 if fuel_collection_minutes_07 > 60 & !mi(fuel_collection_minutes_07) // Capping LPG collection time at 60 minutes per day
replace fuel_collection_minutes_08 = . if surveyround > 0 // No collection time requested for electricity in follow-up surveys
replace fuel_collection_minutes_11n2 = . if surveyround == 2 // No collection time requested for trash in second follow-up survey
egen total_fuel_collection_time = rowtotal(fuel_collection_minutes_*), missing
lab var total_fuel_collection_time "Total fuel-collection time, mins. per day"
egen trad_fuel_collection_time = rowtotal(fuel_collection_minutes_01 fuel_collection_minutes_02 fuel_collection_minutes_03 fuel_collection_minutes_04 fuel_collection_minutes_11n2), missing
lab var trad_fuel_collection_time "Traditional-fuel (fuelwood, crop res., leaves, dung) coll. time, mins. per day"
winsor2 total_fuel_collection_time trad_fuel_collection_time, replace cuts(0 97.5) by(surveyround)

* At least one case of cough or cold in past two weeks
foreach var of varlist cough_or_cold_* {
	replace `var' = . if `var' < 0
	}
egen total_cough_or_cold = rowtotal(cough_or_cold_*), missing
lab var total_cough_or_cold "Total cases of cough or cold in past two weeks"
gen at_least_one_coughcold = 1 if total_cough_or_cold > 0 & !mi(total_cough_or_cold)
replace at_least_one_coughcold = 0 if total_cough_or_cold == 0
lab var at_least_one_coughcold "HH had at least one cough/cold case in past two weeks"

* Fuelwood weight
bys hh_id (surveyround) : replace fuelwood_used_weight = . if fuelwood_used_weight[_n-1] == . & surveyround == 1
bys hh_id (surveyround) : replace fuelwood_used_weight = . if fuelwood_used_weight[_n-2] == . & surveyround == 2
replace fuelwood_used_weight = . if fuelwood_used_weight < 0 & !mi(fuelwood_used_weight)

* Problems with any of the intervention stoves
gen problems_with_interv_stoves = 1 if (problems_greenway == 1 | problems_gcoil == 1)
replace problems_with_interv_stoves = 0 if (problems_greenway == 0 & problems_gcoil == 0)
lab var problems_with_interv_stoves "HH reports experiencing problems with intervention stoves"

********************************************************************************

* Identifying non-missing household

bys hh_id (surveyround) : gen sample_household = 1 if lostin2013 == 0 & lostin2014[_n+1] == 0 & surveyround == 1
bys hh_id (surveyround) : replace sample_household = sample_household[_n+1] if surveyround == 0
bys hh_id (surveyround) : replace sample_household = sample_household[_n-1] if surveyround == 2
lab var sample_household "Household in analytical sample (non-missing all three rounds)"

* Only over those households with key outcomes not missing in all periods
egen nomiss = rownonmiss(intervention_stove_own  clean_fuel_daily total_fuel_collection_time)
bys hh_id (surveyround) : gen notmissing = 3 if surveyround == 2 & nomiss == 3 & nomiss[_n-1] == 3 & nomiss[_n-2] == 3
bys hh_id (surveyround) : replace notmissing = notmissing[_n+1] if surveyround == 1
bys hh_id (surveyround) : replace notmissing = notmissing[_n+1] if surveyround == 0
tab nomiss notmissing, miss
drop nomiss notmissing

********************************************************************************

* Fix incorrectly coded hamlets
bys uniquegrp : egen chirag_mean = mean(chirag_strata)
tab chirag_mean
list hh_id uniquegrp gpname if chirag_mean != 0 & chirag_mean != 1
replace chirag_strata = 1 if uniquegrp == 68008
replace chirag_strata = 0 if uniquegrp == 69001
drop chirag_mean

* Fix GP names
bys hh_id (surveyround gpname) : replace gpname = gpname[_N]
replace gpname = trim(gpname)
replace gpname = "Chaugaon Chhina" if gpname == "Chaugaon Chinna"
replace gpname = "Gangakort" if gpname == "Gangarkot"
replace gpname = "Ghiroli" if gpname == "Jhiroli"

* Fix GP codes
replace village = trim(village)
bys village (gp_code) : replace gp_code = gp_code[1] if mi(gp_code) 

********************************************************************************

* Anonymize panel data

drop ///
	hh_member_?? /// Names of household members
	age_year_?? age_month_?? age_day_?? /// Ages of household members
	sex_?? /// Sex of household members
	household_head_?? /// Household head indicator
	cooks_?? /// Primary/secondary cook indicator
	primary_cook_?? /// Primary cook indicator
	education_years_?? /// Education years indicator
	school_indicator_?? /// Attended school indicator
	cough_or_cold_?? /// Cough-or-cold indicator
	child_under_five_?? /// Child-under-five indicator
	maxlclass? // Household latent class indicator 

	// 	village /// Name of village
	// gpname /// Name of gram panchayat
	// districtname /// Name of district
	// blockname /// Name of block

********************************************************************************

* Reorder variables

order BL_stove_own_?? BL_number_stove_own_?? BL_stove_used_?? BL_stove_use_hours_?? BL_stove_use_minutes_?? ///
	R1_stove_own_* R1_number_stove_own_* R1_stove_used_* R1_stove_use_hours_* R1_stove_use_minutes_* ///
	R2_stove_own_* R2_number_stove_own_* R2_stove_used_* R2_stove_use_hours_* R2_stove_use_minutes_*, ///
		a(below_pov_line)

order fuel_use_* fuel_use_regularity_* fuel_collection_minutes_* fuel_expenditure_* fuel_expenditure_units_*, ///
	a(R2_stove_use_minutes_12)

* Label remaining variables

foreach var of varlist R2_number_stove_own_* {
	lab var `var' "E.2. How many of [TYPE] do you own?"
}
foreach var of varlist R2_stove_used_* {
	lab var `var' "E.6. Has this stove been used in the past week?"
}
foreach var of varlist R2_stove_use_hours_* {
	lab var `var' "E.9a.n. For a typical day when this [STOVE] in this season is used, how many hou"
}
foreach var of varlist R1_stove_use_minutes_* R2_stove_use_minutes_* {
	lab var `var' "Mins"
}
lab var fuel_use_regularity_11n2 "E.20. How regularly do you use it?"
lab var fuel_collection_minutes_11n2 "Time spent collecting [FUEL], minutes per day"
lab var fuel_expenditure_11n2 "E.21. How much money do you spend on this fuel each month?"
lab var fuel_expenditure_units_11n2 "(1) Harvest season/ (2) All year"
lab var hh_id "Household ID"
lab var state_code "State code"
lab var uniquegrp "Subcluster code"
lab var districtcode "District code"
lab var blockcode "Block code"
lab var problems_greenway "HH experienced problem with Gway stove"
lab var problems_gcoil "HH experienced problem with Gcoil stove"
lab var maintenance_greenway "HH received maintenance for Gway stove"
lab var maintenance_gcoil "HH received mainteance for Gcoil stove"
lab var children_under_five "Number of children under five"
lab var heard_of_clean_stoves "HH has heard of clean stoves"
lab var heard_of_clean_fuels "HH has heard of clean fuels"
lab var districtname "District name"
lab var blockname "Block name"

********************************************************************************

sort hh_id surveyround
order hh_id surveyround, first
order uniquegrp IDcompleted, a(gpsubcl_code)
order districtname districtcode blockname blockcode, a(state_code)
save "${output}panel_r0_r1_r2.dta", replace
